package com.yzf.wisecoder.config;

import com.yzf.file.storage.properties.StoragePropertiesFactory;
import org.springframework.beans.factory.config.PropertiesFactoryBean;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.unit.DataSize;

import javax.servlet.MultipartConfigElement;
import java.io.IOException;

/**
 * @author jianghuiyong
 * @Description OSS配置
 * @date 2020/5/14 19:48
 */
@Configuration
public class StorageConfig {
    @Bean
    public PropertiesFactoryBean createPropertiesFactoryBean() {
        ClassPathResource resource = new ClassPathResource("storage.properties");
        PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();
        propertiesFactoryBean.setLocation(resource);
        return propertiesFactoryBean;
    }

    @Bean
    public StoragePropertiesFactory createStoragePropertiesFactory() {
        try {
            return new StoragePropertiesFactory(createPropertiesFactoryBean().getObject());
        } catch (IOException e) {
            throw new RuntimeException("get storage properties exception");
        }
    }

    @Bean
    public MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        //允许上传的文件最大值
        factory.setMaxFileSize(DataSize.ofMegabytes(10));
        /// 设置总上传数据总大小
        factory.setMaxRequestSize(DataSize.ofMegabytes(100));
        return factory.createMultipartConfig();
    }
}
